//file create  time 2001-12-26 steven jama.
package ConnDatabase;
import FormatData.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class WriteToxmlBean
{
    public WriteToxmlBean()
    {
	 }
	public String getbyte(String para) throws Exception  {
  String temp=para;
  if (temp!=null){
 //temp =new String(temp.getBytes("iso-8859-1"));
   }
   else{temp="";}
   return temp;
  
  }
	 public String WriteToxml(String createfile,String company,String  uptown_id)
    {
        String s4 = "";
		String valuestr="";
		String SQLstr="";
		String fieldValue="";
		String fieldName="  ";
		  ResultSet resultset=null;
	     try 
	     { 
	       //create.xml
          PrintWriter pw = new PrintWriter(new FileOutputStream(createfile));
          pw.println("<?xml version='1.0' encoding='gb2312' ?> ");//title
          pw.println("<ufinterface roottag='voucher' billtype='' docid='' receiver='u8' sender="+company+" proc='Add' codeexchanged='N' renewproofno='y' exportneedexch='N'>");//col head	      
          try
          {
            ResultSetMetaData rsmd=null;
			SQLstr="SELECT code,company, voucher_type, fiscal_year,";
	        SQLstr+=" accounting_period, voucher_id,"; 
		    SQLstr+=" attachment_number, date, enter, cashier,";  
		    SQLstr+=" signature, checker, posting_date, ";
		    SQLstr+=" posting_person, voucher_making_system, memo1,";
			SQLstr+="  memo2, reserve1, reserve2 FROM dbo.Voucher1 where uptown_id='"+uptown_id+"'";
			SDatabase conn= new SDatabase();
			SDatabase conn1= new SDatabase();
			SDatabase conn2= new SDatabase();
            SDatabase conn3= new SDatabase();
			FormatData format=new FormatData();
			 System.out.println("OK");
			    resultset = conn.executeQuery(SQLstr);
			   
			   int numCols=0;
            if(resultset == null)
            {
                System.out.println("result is nullA");
		      }
		      else
		      {
			   rsmd=resultset.getMetaData();
			   numCols=rsmd.getColumnCount();
			   }
                   String[] fn=new String[19]			   ;
 for (int AA=2;AA<=19;AA++){
fn[AA-2]=rsmd.getColumnName(AA);

}

			   int j=0;
            while(resultset.next()) 
            {
				  valuestr="";
				  fieldValue="";
				  j++;

				pw.println("<voucher id=''>");
				pw.println("<voucher_header>");
				//read code
				fieldValue=getbyte(resultset.getObject(1).toString().trim()); 
				
			    for (int i=2;i<=19;i++)
			    {
               // print voucher_header
	 			fieldName="  ";
 
				fieldName=fn[i-2];	

				 
				valuestr="<"+fieldName+">";

                if (resultset.getObject(i)!=null){
 		
			     String tmp= resultset.getObject(i).toString().trim();

				 if (tmp.indexOf(" ")>0)tmp=tmp.substring(0,tmp.indexOf(" "));
				 if (tmp.indexOf(".")>0)tmp=String.valueOf(format.getFloat1(Float.parseFloat(tmp),3));
				 valuestr+= tmp;
				 }
				valuestr+= "</"+fieldName+">";       
				pw.println(getbyte(valuestr));//  .getBytes("iso-8859-1")
				
				  }
		   System.out.println("result is nullok2"+numCols);
			 pw.println("</voucher_header>");  
				
				  //println voucher body
			  pw.println("<voucher_body>"); 
			  
			  SQLstr="SELECT entry_id, account_code, abstract,";
			  SQLstr+=" settlement, document_id, document_date,currency,";
			  SQLstr+=" unit_price, exchange_rate1, exchange_rate2,";
			  SQLstr+=" debit_quantity, primary_debit_amount, ";
			  SQLstr+=" secondary_debit_amount,natural_debit_currency, credit_quantity,";
			  SQLstr+=" primary_credit_amount ,";
              SQLstr+="secondary_credit_amount,natural_credit_currency,";
			  SQLstr+=" bill_type, bill_id, bill_date,dept_id FROM dbo.voucher2";
			  SQLstr+=" where code="+fieldValue +" order by id,natural_debit_currency desc";
 
			  ResultSet rs = conn1.executeQuery(SQLstr);
  System.out.println(SQLstr);
			  ResultSetMetaData rsmd1=null;
			   if(rs == null)
             {
                System.out.println("result is null"+fieldValue);
		      }
		      else
		      {
			   rsmd1=rs.getMetaData();
			   numCols=rsmd1.getColumnCount();
			   }
			   int iii=1;
			   String dept="";
			  while(rs.next()) 
              {
				
			    valuestr="";
			    fieldValue="";
			    pw.println("<entry>");
  pw.println("<entry_id>"+iii+"</entry_id>");
            
                               iii++;
	 		    for (int i=2;i<=numCols;i++)
			    {
               // print voucher_header
	 			fieldName="  ";
				 System.out.println("a3="+i);
				fieldName=rsmd1.getColumnName(i);	
				valuestr="<"+fieldName+">";
				if (rs.getObject(i)!=null)		
				{
			     String tmp= getbyte(rs.getObject(i).toString().trim());
                 if (fieldName.equals("bill_type")&&tmp.equals("3")) dept="0";
				 else dept="";
				 if (tmp.indexOf(" ")>0)tmp=tmp.substring(0,tmp.indexOf(" "));
				 if (tmp.indexOf(".")>0)tmp=String.valueOf(format.getFloat7(Double.parseDouble(tmp),2));
				 valuestr+= tmp;
				 }
				valuestr+= "</"+fieldName+">";   
			      
				pw.println(valuestr);//  
				  }
                String dept_id="";
				dept_id=rs.getString("dept_id");
				pw.println(" <auxiliary_accounting>");
				pw.println("<item name='dept_id'>"+dept_id+"</item>");
				pw.println("<item name='personnel_id'></item>");
				pw.println("<item name='cust_id'></item>");
				pw.println("<item name='supplier_id'></item>");
				pw.println("<item name='item_id'></item>");
				pw.println("<item name='item_class'></item>");
				pw.println("<item name='operator'></item>");
				pw.println("<item name='operator'/>");
				pw.println("<item name='operator'/>");
				pw.println("<item name='self_define1'></item>");
				pw.println("<item name='self_define2'></item>");
				pw.println("<item name='self_define3'></item>");
				pw.println("<item name='self_define4'></item>");
				pw.println("<item name='self_define5'></item>");
				pw.println("<item name='self_define6'></item>");
				pw.println("<item name='self_define7'></item>");
				pw.println("<item name='self_define8'></item>");
				pw.println("<item name='self_define9'></item>");
				pw.println("<item name='self_define10'></item>");
				pw.println("<item name='self_define11'></item>");
				pw.println("<item name='self_define12'></item>");
				pw.println("<item name='self_define13'></item>");
				pw.println("<item name='self_define14'></item>");
				pw.println("<item name='self_define15'></item>");
				pw.println("<item name='self_define16'></item>");
				pw.println("</auxiliary_accounting>");
				pw.println("<detail>");
				pw.println("<cash_flow_statement>");
				pw.println("</cash_flow_statement>");
				pw.println("</detail>");
		        pw.println("</entry>");
				System.out.println("lenon");
				}
				rs.close();
				 SQLstr=""; 
                          pw.println("</voucher_body>");
	pw.println("</voucher>");
			 }
			//pw.println("</voucher_body>");
	//pw.println("</voucher>");
pw.println("</ufinterface>");
		
				s4="ok";
				resultset.close();
          } catch(Exception exception)
            {
            System.out.println(exception.getMessage()+"ERROR"+SQLstr);
            }
			
		    pw.close();
       } catch(IOException e) {
         System.out.println(e.getMessage());
       } 
       return s4;
    }
}
